﻿using System.Drawing;

namespace Dot5Demo.MarkDowngm
{
    public abstract class MDElement
    {
        public string Text { get; set; }
        public bool IsBold { get; set; }
        public bool IsMathFormula { get; set; }
        public bool UseColor { get; set; }
        private Color _collor = Color.Black;
        public Color Color { get; set; }
        protected string _cachedString;

        public void ClearCache()
        {
            _cachedString = null;
        }

        public virtual string GetContentString()
        {
            return Text;
        }

        public virtual string GenerateStr()
        {
            if (_cachedString != null)
                return _cachedString;
            var str = GetContentString();
            if (IsMathFormula)
            {
                if (Text.Contains("\n"))
                    str = string.Format("$${0}$$", str);
                else
                    str = string.Format("${0}$", str);
            }
            if (IsBold)
                str = string.Format("*{0}*", str);
            if (UseColor)
            {
                var htmlColorStr = string.Format("#{0:X2}{1:X2}{2:X2}", Color.R, Color.G, Color.B);
                str = string.Format("<font color={0}>{1}</font>", htmlColorStr, str);
            }
            _cachedString = str;
            return str;
        }
    }
}
